# -*- coding: utf-8 -*-
import xml.dom.minidom
import codecs
from django.core.management.base import BaseCommand, CommandError



class Command(BaseCommand):
    """
    get article from digitalocean
    """
    help = 'get article from digitalocean'

    def add_arguments(self, parser):
        #parser.add_argument('--username', type=str,  help='用户名')
        #parser.add_argument('--start', type=int, default=0, help='开始页码')
        #parser.add_argument('--end', type=int, default=3, help='结束页码')
        pass

    def handle(self, *args, **options):
        f = codecs.open(r'E:/workspace/bookloft/bookloft/blog/management/commands/cnblog.xml','r','utf-8')
        data = f.read()
        f.close()
        dom = xml.dom.minidom.parseString(data)
        templates = []
        for t in dom.getElementsByTagName('template'):
            template = dict(id = t.getAttribute('id'))
            for child in t.childNodes:
                #if child.nodeName!='#text':
                if isinstance(child,xml.dom.minidom.Element):
                    # template[child.nodeName] = ''.join([text.data for text in child.childNodes if text.nodeName == '#text' or text.nodeName=='#cdata-section'])
                    template[child.nodeName] = ''.join([text.data for text in child.childNodes if isinstance(text,xml.dom.minidom.Text)])
            templates.append(template)
        authors = []
        for a in dom.getElementsByTagName('author'):
            author = dict(
                id = a.getAttribute('id'),
                template = a.getAttribute('template')
            )
            authors.append(author)
        print(dict(templates=templates,authors=authors))
